package com.eastjet.equipment.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceWarehouseService;
import com.eastjet.maintenance.domain.DeviceDept;
import com.eastjet.maintenance.domain.DeviceUser;
import com.eastjet.maintenance.domain.DeviceWarehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 设备仓库管理
 * */
@CrossOrigin
@RestController
@RequestMapping("/warehouse")
public class WareHouseController {

 @Autowired
 private DeviceWarehouseService deviceWarehouseService;
    /*
     * 获取设备仓库列表(所有)
     * */
    @RequestMapping("/list")
    public Page<DeviceWarehouse> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                                 @RequestParam(defaultValue = "2") int size,
                                                 @RequestParam(defaultValue ="") String condition
    ) {
        System.out.println("condition:" + condition);
        return  deviceWarehouseService.getWarehouseList(page, size,condition);
    }

    @RequestMapping("/ulist")
    public Page<DeviceUser> getUserList(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "2") int size,
                                            @RequestParam(defaultValue ="") String condition
    ) {
        System.out.println("condition:" + condition);
        return  deviceWarehouseService.getUserList(page, size,condition);
    }

    @RequestMapping("/dlist")
    public Page<DeviceDept> getDeptList(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "2") int size,
                                            @RequestParam(defaultValue ="") String condition
    ) {
        System.out.println("condition:" + condition);
        return  deviceWarehouseService.getDeptList(page, size,condition);
    }

    @RequestMapping ("/add")
    @Transactional
    public String add(@RequestBody Map<String, Object> map) {
        String TableName;
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else {
            DeviceWarehouse deviceWarehouse = new DeviceWarehouse();
            TableName = (String) map.get("TableName");
            deviceWarehouse.setWarehouseName((String) map.get("warehouseName"));
            deviceWarehouse.setWarehouseParent(Integer.valueOf(map.get("warehouseParent").toString()));
            deviceWarehouse.setWarehouseDeptId(Integer.valueOf(map.get("warehouseDeptId").toString()) );
            deviceWarehouse.setWarehouseUserId(Integer.valueOf(map.get("warehouseUserId").toString()));
            if(map.get("warehouseState").toString().equals("停用")){
                deviceWarehouse.setWarehouseState(1);
            }else {
                deviceWarehouse.setWarehouseState(0);
            }

            deviceWarehouse.setNoet((String) map.get("noet"));
            System.out.println("1111111111111111111111111");
            return deviceWarehouseService.addWarehouse(deviceWarehouse,TableName);
        }
    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceWarehouse deviceWarehouse=new DeviceWarehouse();

        deviceWarehouse.setWarehouseId(Integer.valueOf(map.get("warehouseId").toString()));
        deviceWarehouse.setWarehouseName((String) map.get("warehouseName"));
        deviceWarehouse.setWarehouseParent(Integer.valueOf(map.get("warehouseParent").toString()));
        deviceWarehouse.setWarehouseDeptId(Integer.valueOf(map.get("warehouseDeptId").toString()) );
        deviceWarehouse.setWarehouseUserId(Integer.valueOf(map.get("warehouseUserId").toString()));
        if(map.get("warehouseState").toString().equals("停用")){
            deviceWarehouse.setWarehouseState(1);
        }else {
            deviceWarehouse.setWarehouseState(0);
        }
        deviceWarehouse.setNoet((String) map.get("noet"));
        return deviceWarehouseService.updateWarehouse(deviceWarehouse);
    }

    @RequestMapping("/delete")
    public String delete(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceWarehouse deviceWarehouse=new DeviceWarehouse();
        deviceWarehouse.setWarehouseId(Integer.valueOf(map.get("warehouseId").toString()));
        return deviceWarehouseService.deleteWarehouse(deviceWarehouse);
    }

    @RequestMapping("/updateState")
    public String updateState(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceWarehouse deviceWarehouse=new DeviceWarehouse();
        deviceWarehouse.setWarehouseId(Integer.valueOf(map.get("warehouseId").toString()));
        deviceWarehouse.setWarehouseState(Integer.valueOf(map.get("warehouseState").toString()));
        return deviceWarehouseService.updateWarehouseState(deviceWarehouse);
    }


}
